<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\user_att;
use App\User;

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $req)
    {
        $user = $req->user();
        if ($user) {
            $name = $user->name;
        }else{   
            $name = 0;       
        }
        $data = ['name'=>$name];
        return view('Home/index',$data);
    }


    public function user(Request $req){

        $name = $req->user()->name;
        if ($name){

            //判断user_atts里是否存在id
            $user = DB::table('users')->where('name',$name)->first();
            $id=$user->id;
            $count=DB::table('user_atts')->where('id',$id)->count();
            //如果不存在就创建
            if (!$count){
                $row=['id'=>$id];
                DB::table('user_atts')->insert($row);
            }
            //联合查询
            $user = DB::table('users')
                    ->leftJoin('user_atts', 'user_atts.id', '=', 'users.id')
                    ->where('name',$name)
                    ->first();
            //显示网页        
            $data = ['user'=>$user];
            return view('Home/user',$data);

        }else{   
            //如果没有用户就跳转到主页
            $name = 0;   
            $data = ['name'=>$name];    
            return view('Home/index',$data);
            
        }
    }




    public function user_edit(Request $req){


        $name = $req->user()->name;
        if ($name){

            //判断user_atts里是否存在id
            $user = DB::table('users')->where('name',$name)->first();
            $id=$user->id;
            $count=DB::table('user_atts')->where('id',$id)->count();
            //如果不存在就创建
            if (!$count){
                $row=['id'=>$id];
                DB::table('user_atts')->insert($row);
            }
            //联合查询
            $user = DB::table('users')
                    ->leftJoin('user_atts', 'user_atts.id', '=', 'users.id')
                    ->where('name',$name)
                    ->first();
            //显示网页        
            $data = ['user'=>$user];
            return view('Home.user_edit',$data);

        }else{   
            //如果没有用户就跳转到主页
            $name = 0;   
            $data = ['name'=>$name];    
            return view('Home.index',$data);
            
        }

        // $name = $req->user()->name;
        // if ($name){
        //     $user = DB::table('users')->where('name',$name)->first();
        //     $data = ['user'=>$user];
        //     return view('Home/user_edit',$data);
        // }else{   
        //     $name = 0;   
        //     $data = ['name'=>$name];    
        //     return view('Home/index',$data);
        // }
    }
    public function edit_post(Request $req,$id){

        $user_att=user_att::where('id',$id)->first();
        $user=User::where('id',$id)->first();


        if (($req->file('user_img'))) {
            $a=date("Y-m-d");
            $b=rand(100,999).'.jpg';
            $req->file('user_img')->move($a,$b);
            $user_att->user_img=$a."/".$b;


        }


        $user_att->gander=$_POST['gander'];
        $user_att->age=$_POST['age'];
        $user_att->nick=$_POST['nick'];
        $user_att->intro=$_POST['intro'];


        $user->email=$_POST['email'];
        $user->mobile=$_POST['mobile'];




        return ($user_att->save() && $user->save()) ? redirect('user') : 'fail';


    }

}
